package com.supermap.census.ui;

import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.supermap.census.R;
import com.supermap.census.app.BaseActivity;
import com.supermap.census.app.SharedPreferencesManager;
import com.supermap.census.event.EventBusModel;
import com.supermap.census.ui.fragment.DataFragment;
import com.supermap.census.ui.fragment.MapFragment;
import com.supermap.census.ui.fragment.MyselfFragment;
import com.supermap.census.utils.IntentUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

/**
 * 地理信息统计的主界面
 */
public class MainHostActivity extends BaseActivity {
    private FragmentTabHost tabHost;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        tabHost.setBackgroundColor(getResources().getColor(R.color.white));
        //使用fragment代替activity转换实现
        tabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
        //
        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator(setTabMenu("地图", R.drawable.main_home)), MapFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator(setTabMenu("数据", R.drawable.main_home)), DataFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator(setTabMenu("我的", R.drawable.main_home)), MyselfFragment.class, null);
        /**
         * 去掉分割线
         */
        tabHost.getTabWidget().setDividerDrawable(R.color.colorAccent);
        EventBus.getDefault().register(this);
    }

    //自定义tab
    public View setTabMenu(String name, int image) {
        View v = LayoutInflater.from(MainHostActivity.this).inflate(R.layout.tab_own_item_layout, null);
        v.setBackgroundColor(getResources().getColor(R.color.white));
        TextView menuText = (TextView) v.findViewById(R.id.tab_menu_txt);
        ImageView menuImg = (ImageView) v.findViewById(R.id.tab_com_img);
        menuText.setText("" + name);
        menuImg.setImageResource(image);
        return v;
    }

    @Override
    protected void doBusiness() {

    }

    @Override
    protected void onStart() {
        super.onStart();
        checkIsLogin();
    }

    @Subscribe
    public void isFinish(EventBusModel eventBusModel) {
        if (eventBusModel.isFinish()) {
            finish();
        }
    }

    /**
     * 检查用户是否登陆
     */
    private void checkIsLogin() {
        boolean isLogin = SharedPreferencesManager.getUserIsLogin();
        if (isLogin) {
            //ToastUtil.showToast(getApplicationContext(), "登录了的");
        } else {
            IntentUtils.into(this, LoginActivity.class);
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
